package com.controller;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 和文章相关的业务操作
 */
@Controller
public class ArticleController {
    private static File ARTICLE_DIR = new File("./articles");

    static {
        if (!ARTICLE_DIR.exists()) {//若该目录不存在!
            ARTICLE_DIR.mkdirs();//创建该目录
        }
    }

    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理文章列表页面...");
        //1
        List<Article> articleList = new ArrayList<>();
        File[] subs = ARTICLE_DIR.listFiles(f -> f.getName().endsWith(".obj"));
        for (File file : subs) {
            try (
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList.size());

        //生成动态页面
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article : articleList) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");


        System.out.println("处理文章列表页面完毕...");


    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章...");
        //1获取表单上的信息
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);
        //必要的验证工作
        if (title == null || author == null || content == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        //2
        File articleFile = new File(ARTICLE_DIR, title + ".obj");
        if (articleFile.exists()) {//文件存在说明是重复的标题
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(new Article(title, author, content));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3
        File file = new File("./webapps/myweb/article_success.html");
        response.setEntity(file);

        System.out.println("处理发表文章完毕...");
    }
}
